package com.lfz.quality_effect.service.impl;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfz.quality_effect.exception.MyException;
import com.lfz.quality_effect.mapper.TVerifyStatisticsMapper;
import com.lfz.quality_effect.model.dto.VerifyStatisticsDTO;
import com.lfz.quality_effect.model.entity.TVerifyStatistics;
import com.lfz.quality_effect.model.vo.VerifyOptimizeVO;
import com.lfz.quality_effect.service.TVerifyStatisticsService;
import com.lfz.quality_effect.utils.ExcelListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TVerifyStatisticsServiceImpl extends ServiceImpl<TVerifyStatisticsMapper, TVerifyStatistics> implements TVerifyStatisticsService {


    /**
     * 上传速裁快审统计信息
     * @param file
     * @param
     */
    @Override
    public boolean uploadVerifyStatistics(MultipartFile file) {


        String dataTime = "";
        boolean result = false;

        ExcelListener listener = new ExcelListener();
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //传入参数
        ExcelReader excelReader = new ExcelReader(inputStream,null, listener);
        Sheet sheet = new Sheet(1, 1, VerifyStatisticsDTO.class);

        //读取信息
        excelReader.read(sheet);
        List<Object> objectList =listener.getDatas();

        ArrayList<TVerifyStatistics> tList = new ArrayList<>();
        for (int i = 0; i < objectList.size(); i++) {
            VerifyStatisticsDTO verifyStatisticsDTO = (VerifyStatisticsDTO) objectList.get(i);
            TVerifyStatistics tVerifyStatistics = new TVerifyStatistics();
            dataTime = verifyStatisticsDTO.getDataTime().substring(0, 10);

            tVerifyStatistics.setCourt(verifyStatisticsDTO.getCourt());
            tVerifyStatistics.setDataTime(dataTime);
            tVerifyStatistics.setCaseType(verifyStatisticsDTO.getCaseType());
            tVerifyStatistics.setCaseNo(verifyStatisticsDTO.getCaseNo());
            tVerifyStatistics.setReceiveDate(verifyStatisticsDTO.getReceiveDate());
            tVerifyStatistics.setSettleDate(verifyStatisticsDTO.getSettleDate());
            tVerifyStatistics.setSettleType(verifyStatisticsDTO.getSettleType());
            tVerifyStatistics.setTrialDays(verifyStatisticsDTO.getTrialDays());
            tVerifyStatistics.setUndertakeStatus(verifyStatisticsDTO.getUndertakeStatus().equals("是") ? 1 : 0);
            tVerifyStatistics.setUndertaker(verifyStatisticsDTO.getUndertaker());
            tVerifyStatistics.setCreateTime(LocalDateTime.now());

            tList.add(tVerifyStatistics);
        }

        QueryWrapper<TVerifyStatistics> wrapper = new QueryWrapper<>();
        wrapper.eq("data_time",dataTime);
        Integer count = this.getBaseMapper().selectCount(wrapper);
        // 存在当天的数据
        if (count != null && count > 0){
            log.info("已存在{}的数据",dataTime);
        }
        else {
            result = this.saveBatch(tList);
        }

        return result;

    }

    /**
     * 速裁快审优化
     * @param option
     * @return
     */
    @Override
    public VerifyOptimizeVO verifyOptimize(String option) {
        Integer trialDays;

        switch(option) {
            case "14":
                trialDays = 14;
                break;
            case "7":
                trialDays = 7;
                break;
            default:
                throw new MyException(500,"只能选择优化14天或7天");
        }

        return this.getBaseMapper().verifyOptimize(trialDays);
    }

}
